package action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import service.UserManager;
import service.impl.UserManagerImpl;

public class ListUserAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		UserManager um = new UserManagerImpl();
		int userCount=um.getUserCount();
		int pageCount=um.getPageCount(userCount, um.USER_PAGE_SIZE);
		int pageNo;
		String pageNoStr=request.getParameter("pageNo");
		if(pageNoStr==null || pageNoStr.trim().equals(""));
		{
			pageNo=1;
		}
		try{
			pageNo=Integer.parseInt(pageNoStr.trim());
		}catch(Exception e){
			pageNo=1;
		}
		if(pageNo>pageCount){
			pageNo=pageCount;
		}
		request.setAttribute("no" , (pageNo-1)*um.USER_PAGE_SIZE);
		request.setAttribute("pageCount", pageCount);
		request.setAttribute("currentPage", pageNo);
		request.setAttribute("userList", um.getAllUserByPage(pageNo));
		return mapping.findForward("listUser");
	}

}
